if SERVER then
	AddCSLuaFile( "shared.lua" )
end

if CLIENT then
	SWEP.Slot				= 1
	SWEP.SlotPos			= 2
	SWEP.DrawCrosshair      = false
	SWEP.IconLetter = ""
	SWEP.DrawAmmo = false
--	SWEP.WepSelectIcon = surface.GetTextureID("vgui/entities/ss_keys")
end

SWEP.PrintName			= "Keys"
SWEP.Author				= ""
SWEP.Instructions = "Left click to lock your door.\nRight click to unlock your door."
	
SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.ViewModel			= "models/weapons/v_fists.mdl"
SWEP.WorldModel			= "models/weapons/w_fists.mdl"

SWEP.ViewModelFOV	= 62
SWEP.ViewModelFlip	= false

SWEP.Primary.Sound = "doors/door_latch1.wav"
SWEP.Primary.ClipSize		= - 1
SWEP.Primary.Delay			= 1
SWEP.Primary.DefaultClip	= 0
SWEP.Primary.Automatic		= true
SWEP.Primary.Ammo			= ""

SWEP.Secondary.Sound = "doors/door_latch3.wav"
SWEP.Secondary.ClipSize		= - 1
SWEP.Secondary.DefaultClip	= 0
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo			= ""

function SWEP:Initialize()
		self:SetWeaponHoldType( "normal" )
end

function SWEP:Deploy()
	if( SERVER ) then
		self.Owner:DrawViewModel( false )
		self.Owner:DrawWorldModel( false )
	end
end

function SWEP:PrimaryAttack()
	if ( CurTime() < self.Primary.Delay	) then return end

	self.Primary.Delay = CurTime() + 1
	
	local tr = {}
	tr.start = self.Owner:GetShootPos()
	tr.endpos = tr.start + ( self.Owner:GetCursorAimVector() * 80 )
	tr.filter = self.Owner
	tr = util.TraceLine( tr )

	if ( SERVER ) then
		if ( tr.Entity:IsValid() and tr.Entity:IsDoor() ) then
			if tr.Entity.Owners and ( tr.Entity.Owners == self.Owner:UniqueID()) or ( tr.Entity.CoOwners and table.HasValue(tr.Entity.CoOwners, self.Owner:UniqueID()) ) or (tr.Entity:IsCopOnly() and self.Owner:Team() == 2) then
				tr.Entity:Fire( "lock" )
				tr.Entity.IsLocked = true
				self.Owner:EmitSound( self.Primary.Sound )
				return
			end
			
			if tr.Entity.Owners != nil or "" and ( tr.Entity.Owners != self.Owner:UniqueID()) or ( tr.Entity.CoOwners and !table.HasValue(tr.Entity.CoOwners, self.Owner:UniqueID()) ) then
				self.Owner:SetAnimation(PLAYER_ATTACK1)
				self.Weapon:EmitSound("physics/plastic/plastic_box_impact_hard4.wav",100,70)
			elseif tr.Entity.Owners == nil or "" then
				self.Owner:SetAnimation(PLAYER_ATTACK1)
				self.Weapon:EmitSound("physics/plastic/plastic_box_impact_hard4.wav",100,70)			
			end
		end
	end
end

function SWEP:SecondaryAttack()
	self.Weapon:SetNextSecondaryFire( CurTime() + 1 )
	
	local tr = {}
	tr.start = self.Owner:GetShootPos()
	tr.endpos = tr.start + ( self.Owner:GetCursorAimVector() * 80 )
	tr.filter = self.Owner
	tr = util.TraceLine( tr )
	
	if ( SERVER ) then
		if ( tr.Entity:IsValid() and tr.Entity:IsDoor() ) then
			if tr.Entity.Owners and ( tr.Entity.Owners == self.Owner:UniqueID()) or ( tr.Entity.CoOwners and table.HasValue(tr.Entity.CoOwners, self.Owner:UniqueID()) ) or (tr.Entity:IsCopOnly() and self.Owner:Team() == 2) then
				tr.Entity:Fire( "unlock" )
				tr.Entity.IsLocked = false
				self.Owner:EmitSound( self.Secondary.Sound )
				return
			end
		end
	end
end